﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using BusinessComponents.Implementation;

namespace BusinessComponents
{
    public class BusinessComponentsFacade
    {
        private RoomBLL roomBLL = new RoomBLL();
        private RoomTypeBLL roomTypeBLL = new RoomTypeBLL();
        private BookingBLL bookingBLL = new BookingBLL();
        private GuestBLL guestBLL = new GuestBLL();
        private EmailBLL emailBLL = new EmailBLL();
        private VoucherBLL voucherBLL = new VoucherBLL();

        private bool isValid = true;
        private string errorMessage = string.Empty;

        public bool IsValid
        {
            get { return isValid; }
        }

        public string ErrorMessage
        {
            get { return errorMessage; }
        }

        private void SetError(string message)
        {
            isValid = false;
            errorMessage += message + Environment.NewLine;
        }

        public string MakeReservation(Booking booking)
        {
            string bookingID = bookingBLL.MakeReservation(booking);
            if (string.IsNullOrEmpty(bookingID))
            {
                SetError(bookingBLL.ErrorMessage);
                return null;
            }
            else
            {
                return bookingID;
            }
        }

        public void AddGuest(Guest guest)
        {
            guestBLL.addGuest(guest);
        }

        public void AddRoomType(RoomType roomType)
        {
            RoomTypeBLL rtBLL = new RoomTypeBLL();
            rtBLL.AddRoomType(roomType);

        }

        public IEnumerable<Booking> FindAllBooking()
        {
            BookingBLL bookingBLL = new BookingBLL();
            return bookingBLL.findAllBooking();
        }

        public IEnumerable<Guest> FindAllGuest()
        {
            return guestBLL.FindAllGuest();
        }

        public IEnumerable<Booking> findBookingInRange(DateTime StartDate, DateTime EndDate, String UserName)
        {

            BookingBLL bookingBLL = new BookingBLL();
            return bookingBLL.findBooking(StartDate, EndDate, UserName);
            //IEnumerable<Booking> bookingsByDate = bookingBLL.findBookingInRange(StartDate, EndDate);
            //IEnumerable<Booking> bookingsByName = bookingBLL.FindBookingByGuestName(UserName);
            //IEnumerable<Booking> combined=null;
            //if (!IsEmpty(bookingsByDate) && !IsEmpty(bookingsByName))
            //{
            //    combined = bookingsByDate.Union(bookingsByName);




            //}
            //else
            //    combined = bookingsByDate.Union(bookingsByName);
            //return combined;
            //return bookingsByName;
        }


        public void SendEmail(Booking booking, Guest guest, String HostURL)
        {
            emailBLL.sendEmail(booking, guest, HostURL);
        }

        public IEnumerable<RoomType> findAllRoomType()
        {
            RoomTypeBLL roomTypeBLL = new RoomTypeBLL();
            return roomTypeBLL.findAllTypes();
        }

        public Booking findBooking(int bookingID)
        {
            return bookingBLL.findBooking(bookingID);
        }

        public IEnumerable<Guest> findBookingGuestbyBookingID(int bookingID)
        {
            BookingBLL bookingBLL = new BookingBLL();
            return bookingBLL.findBookingGuestbyBookingID(bookingID);
        }

        public Guest FindById(int id)
        {
            return guestBLL.FindById(id);
        }

        public List<RoomType> FindAvailableRoomType(DateTime startDate, DateTime endDate, int numberOfGuest)
        {
            return roomTypeBLL.FindAvailableRoomType(startDate, endDate, numberOfGuest);
        }

        public IEnumerable<Booking> FindBookingByGuestId(int guestId)
        {
            return bookingBLL.FindBookingByGuestID(guestId);
        }

        public Guest FindGuestByID(int ID)
        {
            return guestBLL.FindById(ID);
        }

        public bool isBookingCancelable(int id)
        {
            return bookingBLL.isBookingCancelable(id);
        }

        public bool isBookingUpdateable(int id)
        {
            return bookingBLL.isBookingUpdateable(id);
        }

        public void SendCancelEmail(Booking booking, Guest guest, String HostURL)
        {
            emailBLL.sendCancelEmail(booking, guest, HostURL);
        }

        public bool ValidateVoucherNumber(string voucherNumber)
        {
            return voucherBLL.ValidateVoucherNumber(voucherNumber);
        }

        public Voucher GetVoucher(string voucherNumber)
        {
            return voucherBLL.getVoucher(voucherNumber);
        }

        public RoomType FindRoomType(int roomTypeID)
        {
            return roomTypeBLL.FindByID(roomTypeID);
        }

        public Booking FindByBookingNumber(string bookingNumber)
        {
            return bookingBLL.FindByBookingNumber(bookingNumber);
        }
    }
}
 